﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ITunesSolver.Helpers;
using System.IO;
using System.Collections.ObjectModel;
using ITunesSolver.Helpers.DataVirtualization;
using System.Threading;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Threading;
using ITunesSolver.Data;

namespace ITunesSolver.UI
{
    /// <summary>
    /// Interaction logic for ITunesLibraryTab.xaml
    /// </summary>
    public partial class ITunesLibraryTab : UserControl
    {
        public ITunesLibrary ITunesLibrary { get; set; }

        public ITunesLibraryTab()
        {
            InitializeComponent();
        }

        private void InitializeSongTab()
        {
            if (ITunesLibrary.Songs != null)
            {
                lvTracks.Initialize(ITunesLibrary.Songs);
            }
        }

        private void InitializePlaylistTab()
        {
            if (ITunesLibrary.Playlists != null)
            {
                lvPlaylists.Initialize(ITunesLibrary.Playlists);
            }
        }

        private void InitializeMetaDataTab()
        {
            if (ITunesLibrary.MetaData != null)
            {
                tbApplicationVersion.Text = ITunesLibrary.MetaData.ApplicationVersion;
                tbFeatures.Text = ITunesLibrary.MetaData.Features.ToString();
                tbLibraryPersistentId.Text = ITunesLibrary.MetaData.LibraryPersistentId;
                tbMajorVersion.Text = ITunesLibrary.MetaData.MajorVersion.ToString();
                tbMinorVersion.Text = ITunesLibrary.MetaData.MinorVersion.ToString();
                tbMusicFolder.Text = ITunesLibrary.MetaData.MusicFolder;
                chkShowContentRating.IsChecked = ITunesLibrary.MetaData.ShowContentRatings;
            }
        }

        private delegate void DummyDelegate();

        internal void Initialize(ITunesLibrary iTunesLibrary)
        {
            ITunesLibrary = iTunesLibrary;

            if (ITunesLibrary.FileName == null || ITunesLibrary.FileName == string.Empty) return;
            LoadITunesLibrary();
        }

        private void LoadITunesLibrary()
        {
            Trace.WriteLine("LoadITunesLibrary START");

            BackgroundWorker loadItunesLibrary = new BackgroundWorker();
            loadItunesLibrary.DoWork += new DoWorkEventHandler(LoadItunesLibrary_DoWork);
            loadItunesLibrary.RunWorkerCompleted += new RunWorkerCompletedEventHandler(LoadItunesLibrary_RunWorkerCompleted);
            loadItunesLibrary.RunWorkerAsync();

            Trace.WriteLine("LoadITunesLibrary DONE");
        }

        void LoadItunesLibrary_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BackgroundWorker metaDataWorker = new BackgroundWorker();
            metaDataWorker.DoWork += new DoWorkEventHandler(MetaDataWorker_DoWork);
            metaDataWorker.RunWorkerAsync();

            BackgroundWorker tracksWorker = new BackgroundWorker();
            tracksWorker.DoWork += new DoWorkEventHandler(TracksWorker_DoWork);
            tracksWorker.RunWorkerAsync();

            BackgroundWorker playlistsWorker = new BackgroundWorker();
            playlistsWorker.DoWork += new DoWorkEventHandler(PlaylistsWorker_DoWork);
            playlistsWorker.RunWorkerAsync();
        }

        void LoadItunesLibrary_DoWork(object sender, DoWorkEventArgs e)
        {
            ITunesHelper.LoadITunesLibrary(ITunesLibrary);
        }

        void PlaylistsWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Trace.WriteLine("InvokePlaylistsLayout START");
            Dispatcher.BeginInvoke(new DummyDelegate(InitializePlaylistTab), DispatcherPriority.Background);
            Trace.WriteLine("InvokePlaylistsLayout DONE");
        }

        void TracksWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Trace.WriteLine("InvokeTracksLayout START");
            Dispatcher.BeginInvoke(new DummyDelegate(InitializeSongTab), DispatcherPriority.Background);
            Trace.WriteLine("InvokeTracksLayout DONE");
        }

        void MetaDataWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Trace.WriteLine("InvokeInitializeMetaDataLayout START");
            Dispatcher.BeginInvoke(new DummyDelegate(InitializeMetaDataTab), DispatcherPriority.Background);
            Trace.WriteLine("InvokeInitializeMetaDataLayout DONE");
        }
    }
}
